from scapy.all import IP, ICMP, sr1
from random import randint
from ipaddress import ip_network
from concurrent.futures import ThreadPoolExecutor
import time

def ping_single(ip):
    ip_id = randint(1, 65535)
    icmp_id = randint(1, 65535)
    icmp_seq = randint(1, 65535)
    packet = IP(dst=str(ip), ttl=64, id=ip_id) / ICMP(id=icmp_id, seq=icmp_seq)
    response = sr1(packet, timeout=1, verbose=False)
    if response:
        print(f"[+] {ip} is alive")

def ping_scan(network):
    net = ip_network(network)
    with ThreadPoolExecutor(max_workers=256) as executor:
        futures = [executor.submit(ping_single, ip) for ip in net.hosts()]
    for future in futures:
        future.result()

if __name__ == '__main__':
    network = "192.168.1.0/24"  # 替换为你的目标网段
    start_time = time.time()
    ping_scan(network)
    end_time = time.time()
    print(f"[+] Scan completed in {end_time - start_time:.2f} seconds")